﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom.Compiler;
using System.Windows.Forms;
using System.Reflection;
using IronPython.Compiler;
using System.IO;
using IronPython.CodeDom;
using IronPython.Hosting;
using Microsoft.Samples.VisualStudio.IronPython.CompilerTasks;

namespace Plugin.SourceEdit
{
    class PythonCompiler
    {
        /*
        public bool CompileSource(string source,string dllname )
        {
            var engine = IronPython.Hosting.Python.CreateEngine();// CreateEngine();
            engine.CreateScope();
            engine.CreateScriptSourceFromString(source);// Execute();
          //  Console.ReadLine();
            return true;

        }
        public void RunAssemble(string dllname)
         {

         }
    }
    */

        IronPython.Hosting.PythonCompiler _compiler ;
        CompilerParameters _objCompilerParameters;
        public PythonCompiler()
        {
             _objCompilerParameters = new CompilerParameters();

            _objCompilerParameters.ReferencedAssemblies.Add("System.dll");
            _objCompilerParameters.ReferencedAssemblies.Add("System.Core.dll");
            _objCompilerParameters.ReferencedAssemblies.Add("IronPython.dll");
            _objCompilerParameters.ReferencedAssemblies.Add("IronMath.dll");



            //_objCompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
            //_objCompilerParameters.ReferencedAssemblies.Add("Tigera.Business.dll");
            //_objCompilerParameters.ReferencedAssemblies.Add("Tigera.IndexChart.dll");
           
            _objCompilerParameters.GenerateInMemory = false;
            _objCompilerParameters.GenerateExecutable = false;

           // if (_compiler.Supports(GeneratorSupport.EntryPointMethod))
            {
                _objCompilerParameters.MainClass = "MainEntery";
            }
            _objCompilerParameters.IncludeDebugInformation = true;
            
        }
        CompilerErrorSink _sink = new CompilerErrorSink();
        public bool CompileSource(string source,string dllname )
        {
            //string dllname = "test" + (i++).ToString() + ".dll";
            _objCompilerParameters.OutputAssembly = dllname + ".dll";
            

            List<string> files = new List<string>() { "e:\\xxx.py" };
            _compiler = new IronPython.Hosting.PythonCompiler(files,dllname,_sink);



            _compiler.Compile();
            
            return _sink.BuildSucceeded;

        }

        public void RunAssemble(string dllname)
        {
             
            var ass = Assembly.LoadFile(dllname);
            var types = ass.GetTypes();
            foreach (var item in types)
            {
                if (item.IsSubclassOf(typeof(CompiledModule)))
                {
                    var func = item.GetMethod("Main");
                    func.Invoke(null,null);
                    //var instance  = ass.CreateInstance(item.Name) as CompiledModule;
                    //var engine = new IronPython.Hosting.PythonEngine();
                    
                   
                }
            }

        }

    }
     //* */
}
